﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jy123.Web.AppCode;
using System.Collections.Generic;
using Jy123.Common;

namespace Jy123.Web.Manage
{
    public partial class Form : MgConfig
    {
        private List<Jy123.Model.Public_JType> listJType;
        private Dictionary<int, string> JTypeDic = new Dictionary<int, string>();
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("Form.aspx");
            string strWhere = "JFather = '2'";
            Jy123.BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
            listJType = BLLJType.GetModelList(strWhere);
            foreach (Jy123.Model.Public_JType JType in listJType)
            {
                JTypeDic.Add(JType.ID, JType.JName);
            }
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_Form form = e.Item.DataItem as Jy123.Model.Public_Form;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = form.ID.ToString();
                Label nameLabel = e.Item.FindControl("formNameLabel") as Label;
                Label JTypeLabel = e.Item.FindControl("JTypeLabel") as Label;
                nameLabel.Text = form.FName;
                JTypeLabel.Text = JTypeDic[form.FJType.Value];
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_Form BLLForm = new Jy123.BLL.Public_Form();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_Form form = BLLForm.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                formNameTextBox.Text = form.FName;

                if (form.FJType.HasValue)
                {
                    JTypeDropDown.SelectedValue = form.FJType.Value.ToString();
                }
                iconImg.Src = form.FModel;
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLForm.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_Form BLLForm = new Jy123.BLL.Public_Form();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_Form form = new Jy123.Model.Public_Form();
                SetValue(form);
                form.ID = BLLForm.Add(form);
                if (fileLogo.PostedFile != null)
                {
                    form.FModel = CreateLogo(form.ID);
                }
                BLLForm.Update(form);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_Form form = BLLForm.GetModel(id);
                SetValue(form);
                if (fileLogo.PostedFile != null)
                {
                    string strLogo = CreateLogo(id);
                    if (!string.IsNullOrEmpty(strLogo))
                    {
                        form.FModel = strLogo;
                    }
                }
                BLLForm.Update(form);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            formNameTextBox.Text = string.Empty;
        }

        private void SetValue(Jy123.Model.Public_Form form)
        {
            form.FName = formNameTextBox.Text;
            form.FJType = int.Parse(JTypeDropDown.SelectedValue);
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_Form BLLForm = new Jy123.BLL.Public_Form();
            List<Jy123.Model.Public_Form> listForm = BLLForm.GetModelList(string.Empty);
            formRepeater.DataSource = listForm;
            formRepeater.DataBind();
            formRepeater.Visible = listForm.Count > 0;
            BindDropDown(listJType);
        }

        private void BindDropDown(List<Jy123.Model.Public_JType> listJType)
        {
            JTypeDropDown.Items.Clear();
            JTypeDropDown.DataSource = listJType;
            JTypeDropDown.DataTextField = "JName";
            JTypeDropDown.DataValueField = "ID";
            JTypeDropDown.DataBind();
            JTypeDropDown.Items.Insert(0, new ListItem("请选择", "-1"));
        }


        protected string CreateLogo(int id)
        {
            CreateDir createDir = new CreateDir();
            HttpPostedFile postFile = fileLogo.PostedFile;
            string logoPath = postFile.FileName;
            string logoName = string.Empty;
            if (!string.IsNullOrEmpty(logoPath))
            {
                string logoType = logoPath.Substring(logoPath.LastIndexOf(".") + 1);
                logoName = createDir.CreateMaintainPath(CreateDir.FORM, id, logoType);
                string mapPath = Server.MapPath("/");

                Upload upload = new Upload();
                upload.PostedFile = postFile;
                upload.FileLength = postFile.ContentLength;
                upload.FileName = logoName;
                upload.Extension = logoType;
                upload.SavePath = mapPath;
                upload.UploadStart(logoName);
            }
            return logoName;
        }
    }
}
